<template>
  <div id="parent">
    <div class="container" v-show="!isPasswordLogin">
      <div class="login-box">
        <h2>登录</h2>
        <div class="textbox">
          <input placeholder="你的手机号" v-model="phone" id="phoneInput"/>
          <SXQGetCodeButton :callback="getCode"/>
        </div>
        <div class="textbox">
          <input placeholder="验证码" v-model="code" id="codeInput"/>
        </div>
        <SXQButton text="登录" :callback="phoneLogin"/>
        <a @click="isPasswordLogin = true">密码登录</a>
      </div>
    </div>

    <div class="container" v-show="isPasswordLogin">
      <div class="login-box">
        <h2>登录</h2>
        <div class="textbox">
          <input placeholder="你的手机号" v-model="phone" id="codeInput"/>
        </div>
        <div class="textbox">
          <input placeholder="密码" type="password" v-model="password" id="codeInput"/>
        </div>
        <SXQButton text="登录" :callback="passwordLogin"/>
        <a @click="isPasswordLogin = false">验证码登录</a>
      </div>
    </div>
  </div>
</template>

<script>
import SXQButton from "@/components/SXQ-Button";
import {getCode, passwordLogin, phoneLogin} from "@/api/api";
import SXQGetCodeButton from "@/components/SXQ-GetCodeButton";
export default {
  name: "LoginPage",
  components: {SXQGetCodeButton, SXQButton},
  data() {
    return {
      phone: "18505021618",
      code: null,
      isPasswordLogin: false,
      password: ""
    }
  },
  methods: {
    passwordLogin() {
      passwordLogin(this.phone, this.password).then(res => {
        this.loginSuccess(res)
      })
    },
    loginSuccess(res) {
      let body = res.body
      if (body.code == 1) {
        // 将数据存入sessionStorage中
        sessionStorage.setItem("user", JSON.stringify(body.data))
        sessionStorage.setItem("token", body.data.token)

        // 登录状态
        sessionStorage.setItem("isLogin", "true")
        this.$store.state.isLogin = "true"
        this.$Message.success("登录成功")
        this.$router.push("/HomePage", () =>{}, () => {})
      } else {
        // 清空SessionStorage中的数据
        this.$Message.error(body.msg)
        this.$store.state.isLogin = null
        sessionStorage.setItem("isLogin", null)
        sessionStorage.setItem("user", null)
      }
    },
    phoneLogin() {
      phoneLogin(this.phone, this.code).then(res => {
        this.loginSuccess(res)
      })
    },

    getCode() {
      getCode(this.phone).then(res => {
        if (res.body.code == 1) {
          this.$Message.success(res.body.data)
        } else {
          this.$Message.error(res.body.msg)
        }
      })
    }
  }
}
</script>

<style scoped>
#parent {
  margin: 0;
  font-family: Arial, sans-serif;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('../../public/imgs/animation/animation1.png');
  background-size: cover;
  background-position: center;
}

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.login-box {
  background: rgba(255, 255, 255, 0.5);
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
  width: 300px;
  transition: 2.5s;
}

.login-box:hover {
  background: rgba(255, 255, 255, 1);
}

h2 {
  text-align: center;
  margin-bottom: 20px;
  color: #333;
}

.textbox {
  margin-bottom: 15px;
}

#codeInput {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
}

#phoneInput {
  width: 50%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
}
</style>